//
//  CommentViewController.m
//  MovieAppforProject1
//
//  Created by ios on 15/7/23.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "CommentViewController.h"
#import "DataService.h"
#import "CommentTableViewCell.h"

@interface CommentViewController ()

@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    [self configureTableView];
}



- (void)configureTableView
{
    _commentTable = [[UITableView alloc] initWithFrame:self.view.bounds];
    _commentTable.delegate = self;
    _commentTable.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"CommentTableViewCell" bundle:[NSBundle mainBundle]];
    [_commentTable registerNib:nib forCellReuseIdentifier:@"commentCell"];
    
    [self.view addSubview:_commentTable];
}

- (void)loadData
{
    _commentList = [[NSArray alloc]init];
    _commenModels = [[NSMutableArray alloc]init];
    
    NSDictionary *commonListDic = [DataService getJsonDataFromFile:@"movie_comment.json"];
    _commentList = [commonListDic objectForKey:@"list"];
    
    for (NSDictionary *dic in _commentList) {
        CommentModel *model = [[CommentModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [_commenModels addObject:model];
        
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_commentList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
    CGRect frame = cell.frame;
  //  frame.size = CGSizeMake(375, <#CGFloat height#>)
    
    cell.commentModel = _commenModels[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger ch;
    UIFont *font = [UIFont systemFontOfSize:15];//11 一定要跟label的显示字体大小一致
    //设置字体
    CGSize size = CGSizeMake(210, 20000.0f);
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    NSString *str = [_commenModels[indexPath.row] valueForKey:@"content"];
    size =[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
    ch = size.height;
    
    return ch + 90;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
